﻿namespace CloudNinja.Metering.Tests.Provider
{
    using System;
    using System.Collections.Generic;

    using CloudNinja.Metering.Common;
    using CloudNinja.Metering.Common.Logging;
    using CloudNinja.Metering.Data.Models;
    using CloudNinja.Metering.Providers.SQLAzure;
    using CloudNinja.Metering.Tests.Support;

    using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;

    using Moq;

    using Xunit;

    public class DatabaseSizeProviderTests
    {
        [Fact]
        public void CanMeterDatabaseSize()
        {
            var loggerMock = new Mock<ILogger>();
            loggerMock.Setup(l => l.LogException(It.IsAny<Exception>()));
            loggerMock.Setup(l => l.LogError(It.IsAny<string>()));
            loggerMock.Setup(l => l.LogInformation(It.IsAny<string>()));

            var repositoryList = new List<MeterData>();

            var meteringRepository = new MeteringRepositoryMock(repositoryList);

            var meteringContextMock = new Mock<IMeteringContext>();
            var strategy = new FixedInterval("fixed", 10, TimeSpan.FromSeconds(3));

            var storageRetryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(strategy);
            var sqlRetryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(strategy);

            meteringContextMock.Setup(mc => mc.AzureStorageRetryPolicy).Returns(storageRetryPolicy);
            meteringContextMock.Setup(mc => mc.SqlAzureCommandRetryPolicy).Returns(sqlRetryPolicy);
            meteringContextMock.Setup(mc => mc.SqlAzureConnectionRetryPolicy).Returns(sqlRetryPolicy);

            var provider = new DatabaseSizeProvider(
                TimeSpan.FromMinutes(1),
                new Dictionary<string, string> { { GlobalTestSettings.DatabaseConnectionString, "tnt_{.+}" } },
                loggerMock.Object,
                meteringRepository,
                meteringContextMock.Object);

            provider.Collect();

            Assert.True(repositoryList.Count > 0);
        }
    }
}
